<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/18/018
 * Time: 14:16
 */

namespace app\common\model;


use think\Exception;

class Brand extends BaseModel
{

    protected $deleteTime = false;

    /**
     * 关联中间表获取分类
     * @return \think\model\relation\HasMany
     */
    public function categorys() {
        return $this->hasMany('CategoryAndBrand');
    }

    /**
     * 获取品牌列表
     * @param array $where
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function getList($where = []) {
        return self::where($where)->order('sort asc, id desc')->paginate(input('param.limit', 15));
    }

    /**
     * 创建或者编辑品牌
     * @param $id
     * @param $param
     * @return \think\response\Json
     * @throws \Exception
     */
    public static function createOrUpdateBrand($param, $id = 0) {
        try {
            if ($id > 0) {
                // 编辑
                $brand = self::get($id);
                if (!$brand) {
                    throw new Exception('品牌不存在');
                }

                $check_name = self::where('name', '=', $param['name'])->where('id', 'neq', $id)->find();
                if ($check_name) {
                    exception('品牌名称已存在');
                }

                $brand->save($param);
            } else {
                $check_name = self::where('name', '=', $param['name'])->find();
                if ($check_name) {
                    exception('品牌名称已存在');
                }
                // 添加
                self::create($param);
            }
            return return_data('success');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }

    /**
     * 修改品牌排序
     * @param $id
     * @param $sort
     * @return \think\response\Json
     */
    public static function saveSort($id, $sort){
        try {
            if ($id > 0) {
                // 编辑
                $brand = self::get($id);
                if (!$brand) {
                    throw new Exception('品牌不存在');
                }
                $brand->save(['sort' => $sort]);
            }
            return return_data('修改成功');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }
    }

    /**
     * 获取推荐品牌，只获取两个
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getRecommendList() {
        $data = self::with('categorys.category')->where('recommend', '=', 1)->limit(2)->select();
        foreach ($data as &$v) {
            $categorys = [];
            if ($v->categorys) {
                foreach ($v->categorys as $c) {
                    if ($c->category) {
                        $categorys[] = $c->category['name'];
                    }
                }
                unset($v->categorys);
            }
            $v->cates = implode(' | ', $categorys);
        }
        return $data;
    }
}